import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import User from '../components/system/User.vue'
import Welcome from '../components/Welcome.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home,
    children: [
      { path: '/system/user', component: User },
      { path: '/system/menu', component: ()=>import('@/components/system/Menu.vue') },
      { path: '/system/role', component: ()=>import('@/components/system/Role.vue') },
      { path: '/system/dic', component: ()=>import('@/components/system/Dic.vue') },
      { path: '/system/log', component: ()=>import('@/components/system/Log.vue') },
      { path: '/system/job', component: ()=>import('@/components/system/Task.vue') }
    ]
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if (to.path == '/login') return next();
  let tokenstr = window.sessionStorage.getItem('token');
  if (!tokenstr) {
    return next('/login');
  }
  return next();
})
export default router
